package com.headwind.logistics.logistics.controller;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.Coupon;
import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.logistics.dto.OrderAddressDTO;
import com.headwind.logistics.logistics.dto.QueryOrderConditionDTO;
import com.headwind.logistics.logistics.pojo.LogiPreRecordedReceipt;
import com.headwind.logistics.logistics.pojo.LogisticsOrderTail;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrder;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrderContent;
import com.headwind.logistics.logistics.service.intl.LogisticsCustomerServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 小恶魔
 * @Date 2020-07-28 20:33
 * @Version 1.0
 * @DESCRIPTION 前台顾客订单控制器
 */
@Controller
@RequestMapping("/logistics/customer")
public class CustomerOrderController {

    //数据访问层
    @Resource
    private LogisticsCustomerServiceIntl logisticsCustomerServiceIntl;

    //api响应统一接口
    @Resource
    private ResultGenerator resultGenerator;

    /**
     * 获取预录单
     * @return
     */
    @GetMapping("queryPrerecord")
    @ResponseBody
    public RestResult queryPrerecord(Integer page, Integer rows, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<LogiPreRecordedReceipt> logiPreRecordedReceipts = logisticsCustomerServiceIntl.queryPrerecordPage(page, rows, customer.getId());
        map.put("data",logiPreRecordedReceipts);
        map.put("pageCount",logisticsCustomerServiceIntl.queryPrerecordPage(null, null, customer.getId()).size());

        return resultGenerator.getSuccessResult(map);
    }

    /**
     * 根据优惠卷id获取优惠卷信息
     * @return
     */
    @GetMapping("queryCouponById/{couponId}")
    @ResponseBody
    public RestResult queryCouponById(@PathVariable("couponId")Long couponId){

        Coupon coupon = logisticsCustomerServiceIntl.queryCouponById(couponId);

        return resultGenerator.getSuccessResult(coupon);
    }

    /**
     * 根据顾客id获取订单信息
     * @return
     */
    @GetMapping("queryOrderAddress")
    @ResponseBody
    public RestResult queryOrderByCustomerId(Integer page,Integer rows,HttpSession session){

        Map<String,Object> map = new HashMap<>();

        //从session中获取顾客信息
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<OrderAddressDTO> orderAddressDTOS =
                logisticsCustomerServiceIntl.queryOrderAddressByCustomerId(customer.getId(), page, rows);
        map.put("data",orderAddressDTOS);
        map.put("pageCount",logisticsCustomerServiceIntl.queryOrderAddressByCustomerId(customer.getId(), null, null).size());
        return resultGenerator.getSuccessResult(map);
    }

    /**
     * 根据订单id获取订单跟踪事件
     * @return
     */
    @GetMapping("queryOrderTailByOrderId/{orderId}")
    @ResponseBody
    public RestResult queryOrderTailById(@PathVariable("orderId")Long orderId){

        List<LogisticsOrderTail> logisticsOrderTails =
                logisticsCustomerServiceIntl.queryOrderTailByOrderId(orderId);


        return resultGenerator.getSuccessResult(logisticsOrderTails);
    }

    /**
     * 添加投诉工单信息
     * @param workOrder
     * @param workOrderContent
     * @param session
     * @return
     */
    @PostMapping("addWorkOrder")
    @ResponseBody
    public RestResult addWorkOrder(LogisticsWorkOrder workOrder, LogisticsWorkOrderContent workOrderContent, HttpSession session){

        //从session中获取顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        //给工单内容实体类赋值顾客对象
        workOrderContent.setCustomerId(customer.getId());

        Integer count = logisticsCustomerServiceIntl.addWorkOrder(workOrder, workOrderContent);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("投诉失败，重新试试");

    }

    /**
     * 根据订单号和顾客id获取工单
     * @param orderNo
     * @param session
     * @return
     */
    @GetMapping("queryWorkOrderByOrderNoAndCustomerId")
    @ResponseBody
    public RestResult queryWorkOrderByOrderNoAndCustomerId(String orderNo,HttpSession session){
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        LogisticsWorkOrder logisticsWorkOrder =
                logisticsCustomerServiceIntl.queryWorkOrderCountByCustomerIdOrOrderNo(orderNo, customer.getId());

        return resultGenerator.getSuccessResult(logisticsWorkOrder);
    }

    /**
     * 修改预录单状态
     * @param orderStatus
     * @param prerecordId
     * @return
     */
    @PostMapping("updatePrerecordById")
    @ResponseBody
    public RestResult updatePrerecordById(Integer orderStatus,Long prerecordId){

        Integer count = logisticsCustomerServiceIntl.updatePrerecordById(orderStatus, prerecordId);

        if (count > 0){
            return resultGenerator.getSuccessResult();
        }
        return resultGenerator.getFailResult("取消失败，重新试试");
    }


    /**
     * 根据条件查询我寄的订单
     * @param queryOrderConditionDTO
     * @param page
     * @param rows
     * @param session
     * @return
     */
    @GetMapping("querySendOrderByNoOrNameOrPhoneOrDate")
    @ResponseBody
    public RestResult querySendOrderByNoOrNameOrPhoneOrDate(QueryOrderConditionDTO queryOrderConditionDTO,Integer page,Integer rows,HttpSession session){
        //从session中获取顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        List<OrderAddressDTO> orderAddressDTOS =
                logisticsCustomerServiceIntl.querySendOrderByNoOrDateOrStatus(queryOrderConditionDTO, customer.getId(), page, rows);


        return resultGenerator.getSuccessResult(orderAddressDTOS);
    }

    /**
     * 获取我收的订单信息
     * @param page
     * @param rows
     * @param session
     * @return
     */
    @GetMapping("queryPutOrder")
    @ResponseBody
    public RestResult queryPutOrder(QueryOrderConditionDTO queryOrderConditionDTO,Integer page,Integer rows,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        //从session中获取顾客对象
        Customer customer = (Customer)session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<OrderAddressDTO> orderAddressDTOS = logisticsCustomerServiceIntl.queryPutOrderByNo(queryOrderConditionDTO,customer, page, rows);
        map.put("data",orderAddressDTOS);
        map.put("pageCount",logisticsCustomerServiceIntl.queryPutOrderByNo(queryOrderConditionDTO,customer, null, null).size());

        return resultGenerator.getSuccessResult(map);
    }

    /**
     * 根据订单号获取订单跟踪信息
     * @param orderNo
     * @param session
     * @return
     */
    @GetMapping("queryOrderTailByOrderNo")
    @ResponseBody
    public RestResult queryOrderTailByOrderNo(String orderNo, HttpSession session, HttpServletResponse response){

        //从session中获取客户对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<LogisticsOrderTail> logisticsOrderTails =
                logisticsCustomerServiceIntl.queryOrderTailByOrderNo(customer.getId(), orderNo);

        return resultGenerator.getSuccessResult(logisticsOrderTails);
    }

}
